export default class PerformanceMonitor {
  constructor() {
    this.metrics = {
      totalMoves: 0,
      totalThinkingTime: 0,
      averageThinkingTime: 0,
      cacheHits: 0,
      cacheMisses: 0,
      cacheHitRate: 0,
      maxDepth: 0,
      nodesEvaluated: 0,
      pruningEfficiency: 0
    }
    
    this.currentMove = {
      startTime: 0,
      endTime: 0,
      depth: 0,
      nodesEvaluated: 0,
      cacheHits: 0,
      cacheMisses: 0
    }
    
    this.reset()
  }
  
  reset() {
    this.metrics = {
      totalMoves: 0,
      totalThinkingTime: 0,
      averageThinkingTime: 0,
      cacheHits: 0,
      cacheMisses: 0,
      cacheHitRate: 0,
      maxDepth: 0,
      nodesEvaluated: 0,
      pruningEfficiency: 0
    }
  }
  
  startMove() {
    this.currentMove = {
      startTime: Date.now(),
      endTime: 0,
      depth: 0,
      nodesEvaluated: 0,
      cacheHits: 0,
      cacheMisses: 0
    }
  }
  
  endMove(depth, nodesEvaluated, cacheHits, cacheMisses) {
    this.currentMove.endTime = Date.now()
    this.currentMove.depth = depth
    this.currentMove.nodesEvaluated = nodesEvaluated
    this.currentMove.cacheHits = cacheHits
    this.currentMove.cacheMisses = cacheMisses
    
    // 更新总体指标
    this.metrics.totalMoves++
    this.metrics.totalThinkingTime += this.currentMove.endTime - this.currentMove.startTime
    this.metrics.averageThinkingTime = this.metrics.totalThinkingTime / this.metrics.totalMoves
    this.metrics.cacheHits += cacheHits
    this.metrics.cacheMisses += cacheMisses
    this.metrics.cacheHitRate = this.metrics.cacheHits / (this.metrics.cacheHits + this.metrics.cacheMisses) * 100
    this.metrics.maxDepth = Math.max(this.metrics.maxDepth, depth)
    this.metrics.nodesEvaluated += nodesEvaluated
    
    // 计算剪枝效率（假设理论最大节点数）
    const theoreticalMaxNodes = Math.pow(15 * 15, depth) // 简化计算
    this.metrics.pruningEfficiency = ((theoreticalMaxNodes - nodesEvaluated) / theoreticalMaxNodes) * 100
  }
  
  getCurrentMoveMetrics() {
    return {
      thinkingTime: this.currentMove.endTime - this.currentMove.startTime,
      depth: this.currentMove.depth,
      nodesEvaluated: this.currentMove.nodesEvaluated,
      cacheHits: this.currentMove.cacheHits,
      cacheMisses: this.currentMove.cacheMisses,
      cacheHitRate: this.currentMove.cacheHits / (this.currentMove.cacheHits + this.currentMove.cacheMisses) * 100
    }
  }
  
  getOverallMetrics() {
    return this.metrics
  }
  
  getPerformanceReport() {
    const current = this.getCurrentMoveMetrics()
    const overall = this.getOverallMetrics()
    
    return {
      currentMove: {
        thinkingTime: `${current.thinkingTime}ms`,
        depth: `${current.depth}层`,
        nodesEvaluated: current.nodesEvaluated.toLocaleString(),
        cacheHitRate: `${current.cacheHitRate.toFixed(1)}%`
      },
      overall: {
        totalMoves: overall.totalMoves,
        averageThinkingTime: `${overall.averageThinkingTime.toFixed(1)}ms`,
        maxDepth: `${overall.maxDepth}层`,
        totalNodesEvaluated: overall.nodesEvaluated.toLocaleString(),
        overallCacheHitRate: `${overall.cacheHitRate.toFixed(1)}%`,
        pruningEfficiency: `${overall.pruningEfficiency.toFixed(1)}%`
      }
    }
  }
  
  // 格式化性能报告为可读文本
  formatReport() {
    const report = this.getPerformanceReport()
    let text = 'AI性能报告:\n\n'
    
    text += '当前回合:\n'
    text += `思考时间: ${report.currentMove.thinkingTime}\n`
    text += `搜索深度: ${report.currentMove.depth}\n`
    text += `评估节点: ${report.currentMove.nodesEvaluated}\n`
    text += `缓存命中率: ${report.currentMove.cacheHitRate}\n\n`
    
    text += '总体统计:\n'
    text += `总回合数: ${report.overall.totalMoves}\n`
    text += `平均思考时间: ${report.overall.averageThinkingTime}\n`
    text += `最大搜索深度: ${report.overall.maxDepth}\n`
    text += `总评估节点: ${report.overall.totalNodesEvaluated}\n`
    text += `总体缓存命中率: ${report.overall.overallCacheHitRate}\n`
    text += `剪枝效率: ${report.overall.pruningEfficiency}\n`
    
    return text
  }
} 